<?php
namespace app\exception;

use app\helpers\api\ApiResponse;
use think\db\exception\ModelNotFoundException;
use think\exception\Handle;
use think\Response;
use Throwable;

class ApiHandle extends Handle
{
    use ApiResponse;
    public function render($request, Throwable $e): Response
    {
        if ($e instanceof ApiException){
            return $this->error($e->getMessage(),$e->getCode());
        }
        // 请求异常
        if ($e instanceof ModelNotFoundException) {
            return $this->failed('数据不存在',400);
        }

        // 其他错误交给系统处理
        return parent::render($request, $e);
    }

}